from bs4 import BeautifulSoup
import requests
import urllib
import socket
import os
import threading
import time
import datetime

count = 1
dir = os.path.exists('dd')
if not dir:
    os.makedirs('dd')
os.chdir('dd')


def download( count ):
    url = "http://xonline.vip/filter/movie/page-" + str(count) + "/"
    r = requests.get(url)
    html = r.text
    soup = BeautifulSoup(html, 'html.parser')
    div_items = soup.find_all('div', class_='tn-bxitem')
    for div in div_items:
        atag = div.find('a')
        title = atag.get('title')
        href = "http://xonline.vip" + atag.get('href')
        print(href)
        print(title)
        img = div.find('img').get('src')
        print(img)
        file_name = "list"

        headers = {'DNT': '1', 'Upgrade-Insecure-Requests': '1',
                   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                   'Cache-Control': 'ax-age=0',
                   'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/602.2.14 (KHTML, like Gecko) Version/10.0.1 Safari/602.2.14'}
        data = None
        req = urllib.request.Request(img, data, headers)
        try:
            response = urllib.request.urlopen(req, timeout=5)
            respHtml = response.read()
            binfile = open(str(title) + ".jpg", "wb")
            binfile.write(respHtml)
            binfile.close()
            with open(file_name + '.txt', 'a') as file:
                file.write("title: " + title + "\n")
                file.write("url: " + href + "\n")
        except socket.timeout:
            print("pass")
for count in range(1, 171):
    t1 = threading.Thread(target=download, args=[count])
    t1.start()
    count += 1
# os.chdir('dd')
# f=open('list.txt', 'r')
# socket.setdefaulttimeout(10)
# while True:
#     line = f.readline()
#     if line:
#         print(line)
#     r = requests.get(line)
#     html = r.text
#     soup = BeautifulSoup(html, 'html.parser')
#     title = soup.title.string
#     print(title)
#     imgdiv = soup.find('div',class_='tn-contentmt').find('img')
#     imgsrc= imgdiv.get('src')
#     print(imgsrc)
#     headers = {'DNT': '1', 'Upgrade-Insecure-Requests': '1',
#                'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#                'Cache-Control': 'ax-age=0',
#                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/602.2.14 (KHTML, like Gecko) Version/10.0.1 Safari/602.2.14'}
#     data = None
#     req = urllib.request.Request(imgsrc, data)
#     try:
#         response = urllib.request.urlopen(req, timeout=10)
#         respHtml = response.read()
#         binfile = open(str(title) + ".jpg", "wb")
#         binfile.write(respHtml)
#         binfile.close()
#     except socket.timeout:
#         print("pass")

